package leetcode;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * 描述:128. 最长连续序列
 *
 * @author Madison You
 * @created 23:00
 */
public class Mid_128_LongestConsecutive {

    public static void main(String[] args) {
        int[] arr = {100,4,200,1,3,2};
        int i = longestConsecutive(arr);
        System.out.println(i);
    }

    public static int longestConsecutive(int[] nums) {
        Set<Integer> set = new HashSet<>();
        for (int i = 0; i < nums.length; i++) {
            set.add(nums[i]);
        }

        int max = 0;
        for (Integer num : set) {
            if (!set.contains(num - 1)) {
                int currentNum = num;
                int currentMax = 1;
                while (set.contains(currentNum + 1)) {
                    currentNum++;
                    currentMax++;
                }
                max = Math.max(max, currentMax);
            }
        }
        return max;
    }

}
